package com.party.core.service.mall.impl;

import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import com.party.common.paging.Page;
import com.party.core.dao.read.mall.MallOrderDetailReadDao;
import com.party.core.dao.write.mall.MallOrderDetailWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.mall.*;
import com.party.core.service.mall.IMallOrderDetailService;
import com.sun.istack.NotNull;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: Administrator.
 * @Description: 商城订单详情
 * @Date:Created in 2017/8/30 0030 上午 9:49.
 */
@Service
public class MallOrderDetailService implements IMallOrderDetailService {
    @Autowired
    private MallOrderDetailReadDao mallOrderDetailReadDao;
    @Autowired
    private MallOrderDetailWriteDao mallOrderDetailWriteDao;


    /**
     * 订单交易插入
     *
     * @param orderDetail 订单交易
     * @return 插入结果（true/false）
     */
    @Override
    public String insert(MallOrderDetail orderDetail) {
        BaseModel.preInsert(orderDetail);
        boolean result = mallOrderDetailWriteDao.insert(orderDetail);
        if (result) {
            return orderDetail.getId();
        }
        return null;
    }

    /**
     * 订单交易更新
     *
     * @param orderDetail 订单交易
     * @return 更新结果（true/false）
     */
    @Override
    public boolean update(MallOrderDetail orderDetail) {
        orderDetail.setUpdateDate(new Date());
        return mallOrderDetailWriteDao.update(orderDetail);
    }

    /**
     * 逻辑删除订单交易
     *
     * @param id 实体主键
     * @return 删除结果(true / false)
     */
    @Override
    public boolean deleteLogic(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return mallOrderDetailWriteDao.deleteLogic(id);
    }

    /**
     * 物理删除订单交易
     *
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean delete(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return mallOrderDetailWriteDao.delete(id);
    }

    /**
     * 批量逻辑删除订单交易
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDeleteLogic(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return mallOrderDetailWriteDao.batchDeleteLogic(ids);
    }

    /**
     * 批量物理删除订单交易
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDelete(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return mallOrderDetailWriteDao.batchDelete(ids);
    }

    /**
     * 根据主键获取订单交易信息
     *
     * @param id 主键
     * @return 订单交易信息
     */
    @Override
    public MallOrderDetail get(String id) {
        return mallOrderDetailReadDao.get(id);
    }

    /**
     * 分页查询订单交易信息
     *
     * @param orderDetail 订单交易信息
     * @param page        分页信息
     * @return 订单交易列表
     */
    @Override
    public List<MallOrderDetail> listPage(MallOrderDetail orderDetail, Page page) {
        return mallOrderDetailReadDao.listPage(orderDetail, page);
    }

    /**
     * 查询所有订单交易信息
     *
     * @param orderDetail 订单交易信息
     * @return 订单交易列表
     */
    @Override
    public List<MallOrderDetail> list(MallOrderDetail orderDetail) {
        return mallOrderDetailReadDao.listPage(orderDetail, null);
    }

    /**
     * 批量查询订单交易信息
     *
     * @param ids         主键集合
     * @param orderDetail 订单交易信息
     * @param page        分页信息
     * @return 订单交易列表
     */
    @Override
    public List<MallOrderDetail> batchList(@NotNull Set<String> ids, MallOrderDetail orderDetail, Page page) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.EMPTY_LIST;
        }
        return mallOrderDetailReadDao.batchList(ids, new HashedMap(), page);
    }

    @Override
    public MallOrderOutputPay findByOrderId(String orderId) {
        MallOrderDetail orderDetail = new MallOrderDetail();
        orderDetail.setOrderId(orderId);
        List<MallOrderOutputPay> orderLists = this.payOrderList(orderDetail, null, null);
        return orderLists.size() > 0 ? orderLists.get(0) : null;
    }

    @Override
    public MallOrderOutputPay findByOrderId(String orderId, String distributorId) {
        MallOrderDetail orderDetail = new MallOrderDetail();
        orderDetail.setOrderId(orderId);
        orderDetail.setDistributorId(distributorId);
        List<MallOrderOutputPay> orderLists = this.payOrderList(orderDetail, null, null);
        return orderLists.size() > 0 ? orderLists.get(0) : null;
    }

    @Override
    public Long getSalesNum(String productId, List<String> productIds) {
        Set<Integer> status = Sets.newHashSet();
        status.add(MallOrderDetailStatus.STATUS_HAVE_SHIPMENT.getCode()); // 待收货
        status.add(MallOrderDetailStatus.STATUS_NOT_SHIPMENT.getCode()); // 待发货
        status.add(MallOrderDetailStatus.STATUS_COMPLETE.getCode()); // 已完成
        status.add(MallOrderDetailStatus.STATUS_NOT_EVALUATE.getCode()); // 待评价
        Long salesNum = mallOrderDetailReadDao.getSalesNum(productId, null, status, productIds);
        return salesNum == null ? 0L : salesNum;
    }

    @Override
    public Long getSuccessNum(String productId) {
        Set<Integer> status = Sets.newHashSet();
        status.add(MallOrderDetailStatus.STATUS_NOT_PAY.getCode()); // 待支付
        status.add(MallOrderDetailStatus.STATUS_NOT_SHIPMENT.getCode()); // 待发货
        status.add(MallOrderDetailStatus.STATUS_HAVE_SHIPMENT.getCode()); // 待收货
        status.add(MallOrderDetailStatus.STATUS_NOT_EVALUATE.getCode()); // 待评价
        status.add(MallOrderDetailStatus.STATUS_COMPLETE.getCode()); // 已完成
        Long successNum = mallOrderDetailReadDao.getSalesNum(productId, null, status, null);
        return successNum == null ? 0L : successNum;
    }

    @Override
    public Long getNotPay(String productId, String goodsId) {
        Set<Integer> status = Sets.newHashSet();
        status.add(MallOrderDetailStatus.STATUS_NOT_PAY.getCode()); // 待支付
        Long successNum = mallOrderDetailReadDao.getSalesNum(productId, goodsId, status, null);
        return successNum == null ? 0L : successNum;
    }

    @Override
    public Long getNotPay(String goodsId, List<String> productIds) {
        Set<Integer> status = Sets.newHashSet();
        status.add(MallOrderDetailStatus.STATUS_NOT_PAY.getCode()); // 待支付
        Long successNum = mallOrderDetailReadDao.getSalesNum(null, goodsId, status, productIds);
        return successNum == null ? 0L : successNum;
    }

    @Override
    public List<MallOrderDetail> findByProductId(String productId) {
        MallOrderDetail orderDetail = new MallOrderDetail();
        orderDetail.setProductId(productId);
        return mallOrderDetailReadDao.listPage(orderDetail, null);
    }

    @Override
    public List<MallOrderOutputParent> listWithLogistics(String orderId, Set<Integer> status) {
        return mallOrderDetailReadDao.listWithLogistics(orderId, status);
    }

    @Override
    public List<MallOrderWithLogisticsOutput> listWithShipment(String targetId) {
        return mallOrderDetailReadDao.listWithShipment(targetId);
    }

    @Override
    public List<MallOrderOutputParent> listWithNotShipment(String orderId) {
        return mallOrderDetailReadDao.listWithNotShipment(orderId);
    }

    @Override
    public List<MallOrderOutputPay> webListPage(MallOrderDetail orderDetail, Map<String, Object> params, Page page) {
        return mallOrderDetailReadDao.webListPage(orderDetail, params, page);
    }

    @Override
    public Long getNotRefund(String parentId, Integer status, String orderId) {
        return mallOrderDetailReadDao.getNotRefund(parentId, status, orderId);
    }

    @Override
    public List<MallOrderOutputPay> payOrderList(MallOrderDetail orderDetail, Map<String, Object> params, Page page) {
        return mallOrderDetailReadDao.payOrderList(orderDetail, params, page);
    }

    @Override
    public MallOrderDetail getUniqueByOrderId(String orderId) {
        return mallOrderDetailReadDao.getUniqueByOrderId(orderId);
    }

    @Override
    public List<MallOrderDetail> findParentByOrderId(String orderId) {
        return mallOrderDetailReadDao.findParentByOrderId(orderId);
    }

    @Override
    public List<MallOrderOutputPay> getDistributorOrderList(MallOrderDetail orderDetail, Map<String, Object> params, Page page) {
        return mallOrderDetailReadDao.getDistributorOrderList(orderDetail, params, page);
    }

    @Override
    public List<Map<String, Object>> getRevenueData(Map<String, Object> params) {
        return mallOrderDetailReadDao.getRevenueData(params);
    }

    @Override
    public Map<String, Object> findDataCountByDistributor(String distributorId, Date createDate) {
        return mallOrderDetailReadDao.findDataCountByDistributor(distributorId, createDate);
    }

    @Override
    public Set<Integer> findStatusByOrderId(String orderId) {
        return mallOrderDetailReadDao.findStatusByOrderId(orderId);
    }

    @Override
    public Map<String, Object> countAnalyzeByDate(String startDate, String endDate) {
        return mallOrderDetailReadDao.countAnalyzeByDate(startDate, endDate);
    }

    @Override
    public Double getSumTotal(String memberId) {
        return mallOrderDetailReadDao.getSumTotal(memberId);
    }
}
